$(function(){
	readyclass();
	readysubject();
	readystudent();
	$("#submit1").click(function(){
		/**
		 * 获取数据
		 */
		 var Class = $("#addclass-select").val();
		 var Subject = $("#addsubject-select").val();
		 var cj = $("#inputRrsults").val;
		 table();
		 
	})
	$("#submit2").click(function(){ 
		
		SubmitResult();
	})
})
var subject={};
function readysubject(){
	$.ajax({
 		url : "/blackhouse/subject",
 		type : "post",
 		data:{"code":"10"},
 		dataType : "json",
 		async:false,
 		success : function(e) {
 			subject=e;
 			var html = "";
 			for(var i = 0;i<e.length;i++)
 			{
 				html+="<option value='" + e[i].id + "'>"+e[i].subjectname+"</potion>"
 			}
 			$("#addsubject-select").html(html);
  		}
 	})
}
var Class={};
function readyclass(){
	$.ajax({
		url : "/blackhouse/seeclass",
		type : "post",
		dataType : "json",
		async:false,
		success : function(e) {
			Class=e;
			var html = "";
			for (var i = 0; i < e.length; i++) {
				html += "<option value='" + e[i].id + "'>" + e[i].className
						+ "</option>"
			}
			$("#addclass-select").html(html);
		}
	})
	
}
var student={};
function readystudent(){
	$.ajax({
		url : "/blackhouse/studentinfo",
		data:{
			'code':"4"
		},
		type : "post",
		dataType : "json",
		async:false,
		success : function(e) {
			student=e;
		}
	})
	
}

function table(){
	var SubjectName = $("#addsubject-select option:selected").text();
	var className = $("#addclass-select option:selected").text();
	var strVar="";
	for(var i=0;i<student.length;i++){
		if(className==student[i].stuclassid){
			strVar += "<tr name=\""+student[i].id+"\">";
				strVar += "<td>"+className+"<\/td>";
				strVar += "<td>"+SubjectName+"<\/td>";
				strVar += "<td>"+student[i].stuCode+"<\/td>";
				strVar += "<td>"+student[i].stuName+"<\/td>";
				strVar += "<td> <input type=\"text\" class=\"form-control\" name=\"inputRrsults\" \/><\/td>";
		}
		strVar += "<\/tr>";
	}
	$("#tbody").html(strVar);
}

/**
 * 保存成绩
 */
function SubmitResult(){
	var List = new Array();
	// 遍历每一个tr
	$("#tbody").find("tr").each(function(){
		var obj = new Object();
		obj.sid=$(this).attr("name");
		obj.result = $(this).find("[name=inputRrsults]").val();
		obj.subid = $("#addsubject-select option:selected").val();
        if(obj.result !== ""){
        List.push(obj);	
        }
    })
    $.ajax({
		url : "/blackhouse/addresult",
		type : "post",
		data : {'info':JSON.stringify(List)},
		dataType : "json",
		success : function(e) {
			if (e!=-1) {
				swal('添加信息成功', '谢谢使用', 'success');
			} else {
				swal('添加信息失败', '未知错误', 'erreo');
			}
		}
    });
    
}




















